package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.article.IArticleClient;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ApAuthorService;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.article.dto.ArticleDto;
import com.heima.model.article.pojo.ApAuthor;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.awt.windows.ThemeReader;

import java.util.Objects;

/**
 * @author Ronaldo
 * @date 2021/8/1
 */
@RestController
@Slf4j
public class ArticleController implements IArticleClient {

    @Autowired
    private ApAuthorService apAuthorService;

    @Autowired
    private ApArticleService apArticleService;

    @Override
    /**
     *根据用户id查询作者信息
     * @param userId
     * @return
     */
    @GetMapping("/api/v1/author/findByUserId/{userId}")
    public ApAuthor findByUserId(@PathVariable("userId") Integer userId) {
        log.info("select author by userId {}" + userId);

        if (Objects.isNull(userId)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return apAuthorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId,userId));
    }

    @Override
    /**
     * 保存作者
     * @param apAuthor
     * @return
     */
    @PostMapping("/api/v1/author/save")
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        log.info("insert with author {}" + apAuthor);

        if (Objects.isNull(apAuthor)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        apAuthorService.save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @GetMapping("/api/v1/author/findByWmUserId/{wmUserId}")
    public ApAuthor findAuthorByWmUserId(@PathVariable("wmUserId") Integer id) {
        log.info("get author by wmUserId{}",id);
        if (Objects.isNull(id)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return apAuthorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getWmUserId,id));
    }

    @Override
    @PostMapping("/api/v1/article/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto articleDto) {
        log.info("save appArticle{}",articleDto);
        if (Objects.isNull(articleDto)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return apArticleService.saveArticle(articleDto);
    }

    @Override
    @GetMapping("/api/v1/author/one/{id}")
    public ApAuthor getApAuthorById(@PathVariable("id") Integer id) {
        log.info("get author by id{}",id);

        if (Objects.isNull(id)){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        return apAuthorService.getById(id);
    }
}
